package data_treating

type User struct {
	UserId      int64  `xorm:"userId"`
	RoomId      int64  `xorm:"roomId"`
	NickName    string `xorm:"nickName"`
	Score       int64  `xorm:"score"`
	FileName    string `xorm:"fileName"`
	CaptureTime int64  `xorm:"captureTime"`
	Created     int64  `xorm:"created"`
	Updated     int64  `xorm:"updated"`
}

type QueryParam struct {
	StartTime int64
	EndTime   int64
}

// 排行榜
func (p *QueryParam) Query() (res []User, err error) {
	err = DB.Select("userId,any_value(nickName) as nickName,SUM(score) AS score").
		Where("captureTime >= ? and captureTime <= ?", p.StartTime, p.EndTime).
		GroupBy("userId").
		OrderBy("score desc").
		Find(&res)
	return
}

func (p *User) Insert() (err error) {
	var list []User
	if err = DB.Where("captureTime = ? and roomId = ?", p.CaptureTime).Find(&list); err != nil {
		return
	}
	_, err = DB.Insert(p)
	return
}

func Delete() (err error) {
	var user = new(User)
	_, err = DB.Where("1 = 1").Delete(user)
	return
}
